﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Xml;

namespace Evan.Config
{
    /// <summary>
    /// WebConfig 操作
    /// </summary>
    public static class WebConfig
    {
        
        #region 获取webconfig配置
        /// <summary>
        /// 获取WebConfig中AppSetting的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key].ToString();
        }

        /// <summary>
        /// 获取AppSetting中的配置项 , 传入默认值 , 如果获取出错或者未配置  , 即使用默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key, string defaultValue)
        {
            try
            {
                defaultValue = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
            }
            catch (Exception ex)
            {
                SystemLog.MemLog.AddExLog2(string.Format("获取WebConfig [{0}] 配置错误  , 将会返回默认值 [{1}]", key, defaultValue), null, ex.ToString());
            }
            return defaultValue;
        }

        /// <summary>
        /// 获取WebConfig中Connection的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnection(string key)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }

        
        #endregion


        //#region 操作WebConfig 配置   注意在编辑WebConfig后会重启程序
        ///// <summary>  
        ///// 修改config文件(AppSetting节点)  
        ///// </summary>  
        ///// <param name="key">键</param>  
        ///// <param name="value">要修改成的值</param>  
        //public static void EditAppSetting(string key, string value)
        //{
        //    Configuration cfa = WebConfigurationManager.OpenWebConfiguration("~");
        //    cfa.AppSettings.Settings[key].Value = value;//修改值
        //    cfa.Save();//保存
        //}
        //public static void AddAppSetting(string key, string value)
        //{
        //    Configuration cfa = WebConfigurationManager.OpenWebConfiguration("~");
        //    cfa.AppSettings.Settings.Add(key, value);//添加值
        //    cfa.Save();//保存
        //}
        //#endregion

        #region 修改config文件
        /// <summary>  
        /// 修改config文件(AppSetting节点)  
        /// </summary>  
        /// <param name="key">键</param>  
        /// <param name="value">要修改成的值</param>  
        public static void EditAppSetting(string key, string value)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径   
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Web.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素   
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性   
                XmlAttribute _key = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素   
                if (_key != null)
                {
                    if (_key.Value == key)
                    {
                        //对目标元素中的第二个属性赋值   
                        _key = nodes[i].Attributes["value"];

                        _key.Value = value;
                        break;
                    }
                }
            }
            //保存上面的修改   
            doc.Save(strFileName);
        }

        /// <summary>  
        /// 修改config文件(ConnectionString节点)  
        /// </summary>  
        /// <param name="name">键</param>  
        /// <param name="value">要修改成的值</param>  
        public static void UpdateConnectionString(string name, string value)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径   
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Web.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素   
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性   
                XmlAttribute _name = nodes[i].Attributes["name"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素   
                if (_name != null)
                {
                    if (_name.Value == name)
                    {
                        //对目标元素中的第二个属性赋值   
                        _name = nodes[i].Attributes["connectionString"];

                        _name.Value = value;
                        break;
                    }
                }
            }
            //保存上面的修改   
            doc.Save(strFileName);
        }
        #endregion 

        

    }
}
